package com.example.guardian.client.spring.comsumer;

import com.example.guardian.client.proxy.InvokerHolder;
import com.example.guardian.client.util.ActionContextUtil;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * 消费者事务拦截器处理
 *
 * @author gang.wang 2021/12/9
 */
public class TransactionConsumerInterceptor implements AfterReturningAdvice {

    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        TransactionConsumer transactionConsumer = method.getAnnotation(TransactionConsumer.class);
        if (transactionConsumer != null) {
            Map<String, Object> context = ActionContextUtil.fetchActionRequestContext(method, args);
            String messageId = context.get("messageId").toString();
            InvokerHolder.get().invokeConsumer(messageId);
        }
    }
}
